import xlrd
import pathlib
import os
import sys

from openpyxl import Workbook
from openpyxl import load_workbook


class summary :
    def __init__(self,file) -> None:
        self.file=file
        self.wb=xlrd.open_workbook(str(file))
        self.wb_active=self.wb.sheets()[0]
        self.wb_row=self.wb_active.nrows
        self.rowPointer=0
        self.writeWb=Workbook()
        self.ws=self.writeWb.active
        self.wp=2
        self.ws.cell(1,1,"代课教师")
        self.ws.cell(1,2,"班级")
        self.ws.cell(1,3,"时间")

        self.main()
        self.saveWb()
    def main(self):
        while(self.rowPointer<self.wb_row):
            rows=self.getrow()
            if(rows[0][-7:]=="老师调代课通知"):
                name_=rows[0][:-7]
                self.write_(name_)
            self.rowPointer+=1
    def saveWb(self):
        path=root/"{0}汇总.xlsx".format(str(self.file)[:-4])
        try:

            self.writeWb.save(path)
        except PermissionError:
            a=input("文件{path}无法保存，请关闭文件！".format(path=path))
            self.saveWb() 
        except Exception as e:
            print(str(e)+"close file")

    def write_(self,name):
        self.rowPointer+=3
        while(True):
            self.rowPointer+=1
            rows=self.getrow() 
            if(rows[0]=="" or rows[3]==""):
                break
            else:
                print(name,rows)
                self.ws.cell(self.wp,1,name)
                self.ws.cell(self.wp,2,rows[0])
                self.ws.cell(self.wp,3,rows[3])
                self.wp+=1
    def getrow(self):
        return self.wb_active.row_values(self.rowPointer)

root=pathlib.Path(__file__).parent
dirs=root.glob('调课通知*.xls')
for f in dirs:
    print(f)
    summary(f)